<?php

namespace app\controller;

use think\facade\Request;
use think\facade\Db;
use think\facade\View;
use think\facade\Session;
use Hashids\Hashids;
use app\AuthController;

use app\model\{
  ContentArticle as ArticleModel,
};

use app\BaseInfoController as BaseInfo;

class Creation extends AuthController
{


  public function trunData()
  {
    $result = ArticleModel::select()->toArray();

    halt($result);
  }



























  ######################################################################################################################################################

  // http://soyul.com/article/new/ckeditor
  // 写文章
  public function newCkeditor()
  {
    $articleId = $this->request->articleId;
    if ($articleId) {
      $article = ArticleModel::where('id', $articleId)->where($this->request->select)->where('editor_type', 'editormd')->find();
      if (!$article)
        $this->redirect('/404');
    }

    return View::fetch('index/new_ckeditor5', [
      'user'    => $this->request->adminInfo,
      'article' => $article ?? null,
    ]);
  }

  ######################################################################################################################################################





  ######################################################################################################################################################

  # 保存文章
  public function newSave()
  {
    $articleId = $this->request->articleId;

    $data['title'] = $this->request->post('title/s');
    $data['content'] = $this->request->post('test_markdown');
    $data['content_html'] = $this->request->post('test_html');
    $data['editor_type'] = $this->request->post('editor/s');

    if ($articleId) {
      $article = ArticleModel::where('id', $articleId)->update($data);
      return json(['state' => 'success', 'str' => '保存成功']);
    } else {
      $data['seller_id'] = $this->request->sellerId; // 当前账号ID
      $data['master_id'] = $this->request->masterId; // 父账号ID
      $data['operator'] = $this->request->operator;  // 操作人
      $data['status'] = 1;
      $data['publish_time'] = date('Y-m-d H:i:s', time());

      $article = ArticleModel::create($data);
      $article->akey = $this->setArticleKey($article->id);
      $article->save();

      return json(['state' => 'insert', 'articleId' => $article->akey, 'str' => '创建成功']);
    }
  }


  // ckeditor5编辑器上传图片
  public function uploadCkeditor5()
  {
    #上传图片，这里缺少了个验证图片的功能
    $file = request()->file('upload');
    $savename = \think\facade\Filesystem::disk('public')->putFile('', $file);
    if ($savename) {
      return json(["uploaded" => 1, "url" => '/storage/' . $savename]);
    } else {
      return json(["uploaded" => 0]);
    }
  }

  ######################################################################################################################################################





}
